#!/usr/bin/env bash

# Base is a collection of general files + commonly included setup functions.

#duplicated from scripts/rvm - rare reload issue
__rvm_has_opt()
{
  if # pre-gnu
   [[ -n "${ZSH_VERSION:-}"  ]]
  then
    setopt | GREP_OPTIONS="" \command \grep "^${1:-}$" >/dev/null 2>&1 || return $?
  elif # mksh
    [[ -n "${KSH_VERSION:-}"  ]]
  then
    set +o | GREP_OPTIONS="" \command \grep "-o ${1:-}$" >/dev/null 2>&1 || return $?
  elif # bash
    [[ -n "${BASH_VERSION:-}" ]]
  then
    [[ ":${SHELLOPTS:-}:" == *":${1:-}:"* ]] || return $?
  else # what is this?!
    return 1
  fi
}

if __rvm_has_opt "errexit"
then rvm_stored_errexit=1
fi

set +e

# duplication marker jkdfkdfngjdjkfngjkdfngjkd
if
  (( ${rvm_trace_flag:-0} == 1 ))
then
  if   [[ -n "${BASH_VERSION:-}" ]]
  then export PS4="+ \$(__rvm_date \"+%s.%N\" 2>/dev/null) \${BASH_SOURCE##\${rvm_path:-}} : \${FUNCNAME[0]:+\${FUNCNAME[0]}()}  \${LINENO} > "
  elif [[ -n "${ZSH_VERSION:-}" ]]
  then export PS4="+ %* %F{red}%x:%I %F{green}%N:%i%F{white} %_"
  fi
  set -o xtrace
fi
export __array_start rvm_path >/dev/null

#
# Setup environment parameters.
#
if [[ -n "${ZSH_VERSION:-}" ]]
then __array_start=1
else __array_start=0
fi

if (( ${rvm_ignore_rvmrc:=0} == 0 ))
then
  [[ -n "${rvm_stored_umask:-}" ]] || export rvm_stored_umask=$(umask)
  rvm_rvmrc_files=("/etc/rvmrc" "$HOME/.rvmrc")
  if [[ -n "${rvm_prefix:-}" ]] && ! [[ "$HOME/.rvmrc" -ef "${rvm_prefix}/.rvmrc" ]]
     then rvm_rvmrc_files+=( "${rvm_prefix}/.rvmrc" )
  fi

  for rvmrc in "${rvm_rvmrc_files[@]}"
  do
    if [[ -f "$rvmrc" ]]
    then
      # pre-gnu
      if GREP_OPTIONS="" \command \grep '^\s*rvm .*$' "$rvmrc" >/dev/null 2>&1
      then
        printf "%b" "
Error:
        $rvmrc is for rvm settings only.
        rvm CLI may NOT be called from within $rvmrc.
        Skipping the loading of $rvmrc"
        return 1
      else
        source "$rvmrc"
      fi
    fi
  done
  unset rvm_rvmrc_files
fi

# duplication marker jdgkjnfnkjdngjkfnd4fd
export rvm_path
if [[ -z "${rvm_path:-}" ]]
then
  if [[ -n "${BASH_SOURCE:-$_}" && -f "${BASH_SOURCE:-$_}" ]]
  then
    rvm_path="${BASH_SOURCE:-$_}"
    rvm_path="$( \command \cd "${rvm_path%/scripts/base}">/dev/null; pwd )"
  elif (( UID == 0 )) && [[ -d "/usr/local/rvm" ]]
  then rvm_path="/usr/local/rvm"
  elif [[ -d "${HOME}/.rvm" ]]
  then rvm_path="${HOME}/.rvm"
  elif [[ -d "/usr/local/rvm" ]]
  then rvm_path="/usr/local/rvm"
  else echo "Can't find rvm install!" 1>&2 ; exit 1
  fi
fi

true ${rvm_scripts_path:="$rvm_path/scripts"}

# Load the general scripts.
# Use rvm_base_except="selector", for example, to override the loading.
load_rvm_scripts()
{
  \typeset -a scripts
  scripts=(
    selector logging support utility init cleanup env rvmrc install environment gemset checksum list
  )

  source "${rvm_scripts_path}/initialize"


  for entry in ${scripts[@]}
  do
    [[ " ${rvm_base_except:-} " == *" $entry "* ]] ||
    source "${rvm_scripts_path}/functions/$entry"  ||
    return $?
  done

  unset rvm_base_except
}
load_rvm_scripts
